package com.application.Tree;



import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.application.Activity.MESApplication;
import com.application.Activity.R;



import android.R.color;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;


public abstract class INode {
	public static final String SPLIT_TEXT = "#";
	public static float noteImageSize=10;
	public static final int FLAG_TITLE=1;
	public static final int FLAG_TEXT=2;
	protected long id;
	protected String text="";
	protected String title="";
	protected String note="";	
	protected float x = 0;
	protected float y = 0;
	protected  int heigh;
	protected  int width;
	protected String user;
	protected int depth=0;
	boolean isZoomIn=true;
	protected long version;
	protected long parentID;
	protected int label;

	protected CirleButton expand;
	protected Bitmap bitmap;
	protected boolean showed=true;
	public abstract void add(INode n);
	public abstract void remove(INode n);	
	public abstract INode getById(long id);
	public abstract ArrayList<INode> getChildren();
	public abstract INode getCollision(float x,float y);
	public abstract INode getExpandCollision(float x,float y);
	public abstract void moveAll(float moveX, float moveY);
	public abstract void autoArrange();
	public abstract void autoArrangeTextNode();
	public abstract int getSumChildren();
	public abstract int getNodeHeight();
	public abstract Element toXmlText(Document doc);
	public abstract INode readXML(Element element);
	public abstract boolean clearChildren();
	public abstract void zoomIn();
	public abstract boolean haveOtherChildren(String myUserName);
	public abstract void onDraw(Canvas canvas);
	public abstract int countLeaf();
	public abstract void expand(boolean expand);
	public abstract void showChildren(boolean show);
	public abstract Element toXmlDelete(Document doc, long id, long vs);
	public abstract String GetStringNode(long vs);
	public abstract ArrayList<INode> getListNode();
	public abstract boolean testExistNode(long id);
	protected INode(){
		this.text="";
		this.title="";
		this.id=-1;
		showed=true;
		
		loadResources();
	}
//	protected INode(long id, String text,float x,float y, String user){
//		this.id=id;
//		this.text=text;
//		this.x=x;
//		this.y=y;
//		this.user=user;
//	}
	protected INode(long id, String title,String text,float x,float y, String user){
		this.id=id;
		this.text=text;
		this.title=title;
		this.x=x;
		this.y=y;
		this.user=user;
		this.generateSize();
		loadResources();
	}
	protected INode(long id, String title,String text,float x,float y, String user, long version, int label, long pID){
		this.id=id;
		this.text=text;
		this.title=title;
		this.x=x;
		this.y=y;
		this.user=user;
		this.version = version;
		this.label = label;
		this.parentID = pID;
		this.generateSize();
		loadResources();
	}
	public static ArrayList<String> Split(String text, int len) {
		ArrayList<String> rs = new ArrayList<String>();
		String []enter=text.split("\\r?\\n");
		for(int k=0;k<enter.length;k++){
		String[] m = enter[k].split(" ");
		String[] line = new String[m.length];
		if (m.length > 0) {
			int n = m.length;
			int j = 0;
			for (int i = 0; i < n; i++) {
				if (m[i].length() >= len) {
					line[j] = m[i];
					j++;
				} else {
					if (line[j] == null) {
						line[j] = m[i];

					} else {
						if ((line[j] + " " + m[i]).length() > len) {
							line[j + 1] = m[i];
							j++;
						} else {
							line[j] += " " + m[i];
							// j++;
						}

					}
				}
			}
		}
		
			for (int i = 0; i < line.length; i++) {
				if (line[i] != null) {
					rs.add(line[i]);
				}
			}
		}
		return rs;
		
	}
	
	protected boolean isCollision(float x, float y, float space) {
		return (x > this.x && x < this.x + width) && (y > this.y-space && y < this.y + heigh+space);
	}
	
	protected void setPostion(float x, float y){
		this.x=x;
		this.y=y;
		if(this.expand!=null){
			expand.setPosition(this.x+this.width+CirleButton.r, this.y+this.heigh/2);
		}
	}
	protected  void generateSize(){
		String mtext=this.user+": "+this.title;
		Paint p=new Paint();
		Rect bounds = new Rect();
		p.getTextBounds(mtext, 0, mtext.length(), bounds);		
		//this.heigh=bounds.height();
		this.heigh=15;
		if(bounds.width()<200){
			this.width=bounds.width()+this.heigh;
		}else{
			this.width=200+this.heigh;
		}
	}
	public void loadResources(){
		bitmap=BitmapFactory.decodeResource(MESApplication.getAppContext().getResources(),R.drawable.node1);
}
		
public  String toParse(){
		StringBuffer buffer=new StringBuffer();
		buffer.append(this.id+SPLIT_TEXT);
		buffer.append(this.title+SPLIT_TEXT);
		buffer.append(this.text+SPLIT_TEXT);
		buffer.append(this.x+SPLIT_TEXT);
		buffer.append(this.y+SPLIT_TEXT);
		buffer.append(this.user+SPLIT_TEXT);
		buffer.append(this.depth+SPLIT_TEXT);
		buffer.append(this.version+SPLIT_TEXT);
		buffer.append(this.label+SPLIT_TEXT);
		buffer.append(this.parentID+SPLIT_TEXT);
		return buffer.toString();
	}
	public  void parse(String s){
		String []m=s.split(INode.SPLIT_TEXT);
		this.id=Long.parseLong(m[0]);
		this.title=m[1];
		this.text=m[2];
		this.x=Float.parseFloat(m[3]);
		this.y=Float.parseFloat(m[4]);
		this.user=m[5];
		this.depth=Integer.parseInt(m[6]);
		this.version=Long.parseLong(m[7]);
		this.label=Integer.parseInt(m[8]);
		this.parentID=Long.parseLong(m[9]);
		this.generateSize();
	}
	public void copy(INode node){
		this.text=node.text;
		this.title=node.title;
		this.version=node.version;
		this.label=node.label;		
	}
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public int getHeigh() {
		return heigh;
	}

	public void setHeigh(int heigh) {
		this.heigh = heigh;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getText() {
		return text;
	}
	public void setText(String newText){
		this.text=newText;				
	}

	public int getDepth() {
		return depth;
	}

	public void setDepth(int depth) {
		this.depth = depth;
	}

	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public long getVersion() {
		return version;
	}
	public void setVersion(long version) {
		this.version = version;
	}
	public long getParentID() {
		return parentID;
	}
	public void setParentID(long parentID) {
		this.parentID = parentID;
	}
	public int getLabel() {
		return label;
	}
	public void setLabel(int label) {
		this.label = label;
	}
	
}
